package se.kayarr.cardplay.shared;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import lombok.Getter;
import se.kayarr.cardplay.gameplay.Targetable;

public final class Util {
	@Getter private static Random random = new Random();
	
	private static DateFormat debugDateFormat = new SimpleDateFormat("[dd MMM yyyy HH:mm:ss.SSS]");
	
	public static void debug(String msg) {
		System.out.println(debugDateFormat.format(new Date()) + " DEBUG>>> " + msg);
	}
	
	public boolean prob(double percent) {
		return (random.nextDouble() * 100.0) < percent;
	}
	
	public static String join(char separator, Object... objs) {
		return join(String.valueOf(separator), objs);
	}
	
	public static String join(String separator, Object... objs) {
		return join(separator, objs);
	}
	
	public static <T> String join(String separator, Iterable<T> items) {
		StringBuilder builder = new StringBuilder();
		for(T obj : items) {
			if(builder.length() != 0) builder.append(separator);
			builder.append(obj);
		}
		return builder.toString();
	}
	
	public static <T> T getItemAtPos(Iterable<? extends T> iterable, int pos) {
		if(pos < 0) throw new IndexOutOfBoundsException();
		
		Iterator<? extends T> iterator = iterable.iterator();
		
		while(iterator.hasNext()) {
			T nextElem = iterator.next();
			if(pos-- == 0) return nextElem;
		}
		
		throw new IndexOutOfBoundsException();
	}
	
	public static String getPrintableTargetFlags(int targetFlags) {
		List<String> stringParts = new LinkedList<>();
		
		if(targetFlags == Targetable.NONE) return "none";
		if(targetFlags == Targetable.ANY_ANY) return "any players or units on fields";
		
		if((targetFlags & Targetable.ALLY_PLAYER) != 0) stringParts.add("yourself");
		if((targetFlags & Targetable.ALLY_UNIT) != 0) stringParts.add("your units on field");
		if((targetFlags & Targetable.OPPONENT_PLAYER) != 0) stringParts.add("opponent players");
		if((targetFlags & Targetable.OPPONENT_UNIT) != 0) stringParts.add("opponent units on field");
		
		return join(", ", stringParts);
	}
	
	public static int getRoundedPercentageOf(int value, double percent) {
		return (int) getPercentageOf(value, percent);
	}
	
	public static double getPercentageOf(double value, double percent) {
		return (percent/100.0)*value;
	}
	
	public static String padAtStart(String orig, int minWidth) {
		return padAtStart(orig, minWidth, ' ');
	}
	
	public static String padAtStart(String orig, int minWidth, char padding) {
		StringBuilder builder = new StringBuilder(orig);
		while(builder.length() < minWidth) {
			builder.insert(0, padding);
		}
		return builder.toString();
	}
	
	public static String padAtEnd(String orig, int minWidth) {
		return padAtEnd(orig, minWidth, ' ');
	}
	
	public static String padAtEnd(String orig, int minWidth, char padding) {
		StringBuilder builder = new StringBuilder(orig);
		while(builder.length() < minWidth) {
			builder.append(padding);
		}
		return builder.toString();
	}
	
	private Util() {};
}
